/**
 * Created by root on 16-12-29.
 */

jQuery(function () {
    table_num();
})

function modify(this_id) {
    var id = $(this_id).parent().parent().children('#td_id').text();
    var nav_id = $(this_id).parent().parent().children('#td_nav_id').text();
    var menu_id = $(this_id).parent().parent().children('#td_menu_id').text();
    var name = $(this_id).parent().parent().children('#td_name').text();
    var order = $(this_id).parent().parent().children('#td_order').text();
    var link = $(this_id).parent().parent().children('#td_link').text();
    var describe = $(this_id).parent().parent().children('#td_describe').text();

    $("[name='id']").val(id);
    $("[name='name']").val(name);
    $("[name='order']").val(order);
    $("[name='describe']").val(describe);
    $("[name='name']").val(name);
    $("[name='link']").val(link);
    $("[name='nav_id']").val(nav_id);
    $("[name='menu_id']").val(menu_id);

    $('#form').attr('action','/nav/configItem/modify');
    $('#add').text('修改');
}


function del(this_id) {
    var id = $(this_id).parent().parent().children("#td_id").text();
    var url = '/nav/configItem/delete';
    $.post({
        url:url,
        data:{id:id}});
    window.location.reload();
}

function change_nav(this_id) {
    var id = $(this_id).val();
    $.get({
        'url':'/nav/configItem/',
        'data':{nav_id:id},
        'success':function (callback) {
            $('tbody').empty();
            $('[name="menu_id"]').empty();
            var table_data = JSON.parse(callback);
            var num = 1;
            table_data['itemData'].forEach(function (data) {
                $('tbody').append('<tr style="text-align: center;height: 25px">'+
                                        '<td style="display: none" id="td_id">'+data["id"]+'</td>'+
                                        '<td style="display: none;" id="td_nav_id">'+data["nav_id"]+'</td>'+
                                        '<td style="display: none;" id="td_menu_id">'+data["menu_id"]+'</td>'+
                                        '<td id="td_num">'+num+'</td>'+
                                        '<td id="td_navName">'+data['nav_name']+'</td>'+
                                        '<td id="td_menuName">'+data['menu_name']+'</td>'+
                                        '<td id="td_name">'+data['name']+'</td>'+
                                        '<td id="td_link">'+data['link']+'</td>'+
                                        '<td id="td_order">'+data['order']+'</td>'+
                                        '<td>'+
                                            '<a style="height: 20px;font-size: 8px;padding-top: 2px;padding-bottom: 1px" class="btn btn-info" onclick="del(this)">删除</a>&nbsp;&nbsp;'+
                                            '<a style="height: 20px;font-size: 8px;padding-top: 2px;padding-bottom: 1px" class="btn btn-info" onclick="modify(this)">修改</a>'+
                                        '</td>'+
                                    '</tr>')
                num += 1;
                                })
            $('[name="menu_id"]').append('<option disabled="disabled" selected="selected">--选择菜单--</option>')
            table_data['menuData'].forEach(function (data) {
                $('[name="menu_id"]').append('<option value='+data['id']+'>'+data['name']+'</option>')
            })
        }
    })
}

function change_menu(this_id) {
    var id = $(this_id).val();
    $.get({
        'url':'/nav/configItem/',
        'data':{menu_id:id},
        'success':function (callback) {
            $('tbody').empty();
            var table_data = JSON.parse(callback);
            var num = 1;
            table_data['itemData'].forEach(function (data) {
                $('tbody').append('<tr style="text-align: center;height: 25px">'+
                                        '<td style="display: none" id="td_id">'+data["id"]+'</td>'+
                                        '<td style="display: none;" id="td_nav_id">'+data["nav_id"]+'</td>'+
                                        '<td style="display: none;" id="td_menu_id">'+data["menu_id"]+'</td>'+
                                        '<td id="td_num">'+num+'</td>'+
                                        '<td id="td_navName">'+data['nav_name']+'</td>'+
                                        '<td id="td_menuName">'+data['menu_name']+'</td>'+
                                        '<td id="td_name">'+data['name']+'</td>'+
                                        '<td id="td_link">'+data['link']+'</td>'+
                                        '<td id="td_order">'+data['order']+'</td>'+
                                        '<td>'+
                                            '<a style="height: 20px;font-size: 8px;padding-top: 2px;padding-bottom: 1px" class="btn btn-info" onclick="del(this)">删除</a>&nbsp;&nbsp;'+
                                            '<a style="height: 20px;font-size: 8px;padding-top: 2px;padding-bottom: 1px" class="btn btn-info" onclick="modify(this)">修改</a>'+
                                        '</td>'+
                                    '</tr>');
                num +=1;
            })
            $('[name="nav_id"]').val(table_data['nav_id']);

        }
    })
}